#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFile>
#include <QFileDialog>

//文件信息
void fileInfo(QString filename)
{
    QFileInfo info(filename);
    bool exists = info.exists();
    bool isDir = info.isDir();
    bool isFile = info.isFile();
    bool isRead = info.isReadable();
    bool isWrite = info.isWritable();
    bool isExec = info.isExecutable();
    info.fileName();
}

//读文件
void MainWindow::on_pushButton_file_read_clicked()
{
    QString filename;

    //选择文件窗口
    filename = QFileDialog::getOpenFileName(nullptr,
                                            "Open File",
                                            "D:\\", /*指定路径*/
                                            "Text File(*.txt *.md);;All File(*.*)");

    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly))
    {
        qDebug() << file.errorString();
        return ;
    }

    // qint64 read(char *data, qint64 maxlen);
    // QByteArray read(qint64 maxlen);
    // QByteArray readAll();
    // qint64 readLine(char *data, qint64 maxlen);
    // QByteArray readLine(qint64 maxlen = 0);
    file.close();


    fileInfo(filename);
}

//写文件
void MainWindow::on_pushButton_file_write_clicked()
{
    QString filename;

    //保存文件窗口
    filename = QFileDialog::getSaveFileName(nullptr,
                                            "Save File",
                                            nullptr, /*不指定路径，即使用上次路径*/
                                            "Text File(*.txt);;All File(*.*)");
    QFile file(filename);
    file.open(QIODevice::WriteOnly);

    // qint64 write(const char *data, qint64 len);
    // qint64 write(const char *data);
    // qint64 write(const QByteArray &data);

    file.close();
}

//创建目录
void MainWindow::on_pushButton_file_creatDir_clicked()
{
    //创建文件夹
    QDir dir;
    if (!dir.exists("E:/Desktop/22/11"))
    {
        dir.mkpath("E:/Desktop/22/11");
    }
}

//选择目录
void MainWindow::on_pushButton_file_getDir_clicked()
{
    QDir dirPath;
    dirPath = QFileDialog::getExistingDirectory(nullptr, "Choose Path", nullptr);
    qDebug() << dirPath.dirName() << dirPath.path();
}
